import math


def sector(data: dict):
    """
    计算扇形，输入一个字典，返回一个字典
    :param data:字典
    {
    r：半径
    l：弧长
    a：圆心角
    c：弦长
    h：弦高
    }

    :return res:字典
    {
    r：半径
    l：弧长
    a：圆心角
    c：弦长
    h：弦高
    "sector_area":扇形面积
    "sector_circumference":扇形周长
    "arcuate_area":弓形面积
    "arcuate_circumference":弓形周长
    "angular_margin_area":角缘面积
    }
    弓形arcuate
    周长、和角缘面积Angular margin area
    """

    if "r" in data and "l" in data:  # 输入半径和弧长
        r = data["r"]
        l = data["l"]
        a = l * 180 / (math.pi * r)
        c = 2 * (r * math.sin(math.radians(a) / 2))
        h = r - (r * r - (c / 2) * (c / 2)) ** (1 / 2)
        sector_area = math.pi * r * r * a / 360
        sector_circumference = 2 * r + a * math.pi * r / 180
        arcuate_area = sector_area - c * (r - h) / 2
        arcuate_circumference = sector_circumference - r - r + c
        angular_margin_area = r * r * (math.tan(math.radians(a / 2)) - math.pi * a / 360)

    elif "r" in data and "a" in data:
        r = data["r"]
        a = data["a"]
        l = a * (math.pi * r) / 180
        c = 2 * (r * math.sin(math.radians(a) / 2))
        h = r - (r * r - (c / 2) * (c / 2)) ** (1 / 2)
        sector_area = math.pi * r * r * a / 360
        sector_circumference = 2 * r + a * math.pi * r / 180
        arcuate_area = sector_area - c * (r - h) / 2
        arcuate_circumference = sector_circumference - r - r + c
        angular_margin_area = r * r * (math.tan(math.radians(a / 2)) - math.pi * a / 360)

    elif "r" in data and "c" in data:
        r = data["r"]
        c = data["c"]
        a = 2 * (math.degrees(math.asin(c / (2 * r))))
        l = a * (math.pi * r) / 180
        h = r - (r * r - (c / 2) * (c / 2)) ** (1 / 2)
        sector_area = math.pi * r * r * a / 360
        sector_circumference = 2 * r + a * math.pi * r / 180
        arcuate_area = sector_area - c * (r - h) / 2
        arcuate_circumference = sector_circumference - r - r + c
        angular_margin_area = r * r * (math.tan(math.radians(a / 2)) - math.pi * a / 360)

    elif "r" in data and "h" in data:
        r = data["r"]
        h = data["h"]
        a = 2 * (math.degrees(math.acos(float(r / h))))
        l = a * (math.pi * r) / 180
        c = math.sqrt(r ** 2 - h ** 2)
        sector_area = math.pi * r * r * a / 360
        sector_circumference = 2 * r + a * math.pi * r / 180
        arcuate_area = sector_area - c * (r - h) / 2
        arcuate_circumference = sector_circumference - r - r + c
        angular_margin_area = r * r * (math.tan(math.radians(a / 2)) - math.pi * a / 360)

    elif "l" in data and "a" in data:
        l = data["l"]
        a = data["a"]
        r = 180 * l / (math.pi * a)
        c = 2 * r * math.sin(a / 2)
        h = r - (r * r - (c / 2) * (c / 2)) ** (1 / 2)
        sector_area = math.pi * r * r * a / 360
        sector_circumference = 2 * r + a * math.pi * r / 180
        arcuate_area = sector_area - c * (r - h) / 2
        arcuate_circumference = sector_circumference - r - r + c
        angular_margin_area = r * r * (math.tan(math.radians(a / 2)) - math.pi * a / 360)

    elif "l" in data and "c" in data:
        l = data["l"]
        c = data["c"]
        a = 90
        r = 2
        h = r - (r * r - (c / 2) * (c / 2)) ** (1 / 2)
        sector_area = math.pi * r * r * a / 360
        sector_circumference = 2 * r + a * math.pi * r / 180
        arcuate_area = sector_area - c * (r - h) / 2
        arcuate_circumference = sector_circumference - r - r + c
        angular_margin_area = r * r * (math.tan(math.radians(a / 2)) - math.pi * a / 360)

    elif "l" in data and "h" in data:
        l = data["l"]
        h = data["h"]
        r = h / 2 + (l * l) / (8 * h)
        a = 180 * l / (r * math.pi)
        c = 2 * r * math.sin(a / 2)
        sector_area = math.pi * r * r * a / 360
        sector_circumference = 2 * r + a * math.pi * r / 180
        arcuate_area = sector_area - c * (r - h) / 2
        arcuate_circumference = sector_circumference - r - r + c
        angular_margin_area = r * r * (math.tan(math.radians(a / 2)) - math.pi * a / 360)

    elif "a" in data and "c" in data:
        a = data["a"]
        c = data["c"]
        r = c / (2 * math.sin(a / 2))
        l = a * math.pi * r / 180
        h = r - (r * r - (c / 2) * (c / 2)) ** (1 / 2)
        sector_area = math.pi * r * r * a / 360
        sector_circumference = 2 * r + a * math.pi * r / 180
        arcuate_area = sector_area - c * (r - h) / 2
        arcuate_circumference = sector_circumference - r - r + c
        angular_margin_area = r * r * (math.tan(math.radians(a / 2)) - math.pi * a / 360)

    elif "a" in data and "h" in data:
        a = data["a"]
        h = data["h"]

        sector_area = math.pi * r * r * a / 360
        sector_circumference = 2 * r + a * math.pi * r / 180
        arcuate_area = sector_area - c * (r - h) / 2
        arcuate_circumference = sector_circumference - r - r + c
        angular_margin_area = r * r * (math.tan(math.radians(a / 2)) - math.pi * a / 360)

    elif "c" in data and "h" in data:
        c = data["c"]
        h = data["h"]
        r = (h * h + (c / 2) * (c / 2)) / (2 * h)
        a = 2 * (math.degrees(math.asin(c / (2 * r))))
        l = a * (math.pi * r) / 180
        sector_area = math.pi * r * r * a / 360
        sector_circumference = 2 * r + a * math.pi * r / 180
        arcuate_area = sector_area - c * (r - h) / 2
        arcuate_circumference = sector_circumference - r - r + c
        angular_margin_area = r * r * (math.tan(math.radians(a / 2)) - math.pi * a / 360)

    else:
        pass
    return {
        "r": r,
        "l": l,
        "a": a,
        "c": c,
        "h": h,
        "sector_area": sector_area,
        "sector_circumference": sector_circumference,
        "arcuate_area": arcuate_area,
        "arcuate_circumference": arcuate_circumference,
        "angular_margin_area": angular_margin_area,
    }


def test():
    """
    测试函数
    :return:

    """

    data = {
        "r": 14,
        "l": 2
    }
    res = sector(data)
    print(res)


test()
